import { _decorator, Component, Node, instantiate } from 'cc';
import { Constant } from '../Common/Constant';
import { ResourceUtil } from '../Common/ResourceUtil';
import { EasyController, EasyControllerEvent } from '../Controller/EasyController';
import { PropType } from '../Controller/PropController';
import { PropItem } from '../UI/PropItem';
import { GameManager } from './GameManager';
const { ccclass, property } = _decorator;

@ccclass('PropItemManager')
export class PropItemManager extends Component {

    propItems: Array<PropItem> = new Array();

    onLoad() {
    }

    start() {
        EasyController.on(EasyControllerEvent.REFRESH_PROP, this.Refresh, this)
    }

    Refresh() {
        for (let i = 0; i < GameManager.Instance.propDatas.length; i++) {
            let item = this.propItems.find(item => item.propType == GameManager.Instance.propDatas[i].propType);
            if (item) {
                if (GameManager.Instance.propDatas[i].count == 0) {
                    item.node.active = false;
                    continue;
                }
                item.RefreshCount(GameManager.Instance.propDatas[i].count);
            } else {
                ResourceUtil.loadPrefab(Constant.Path.PropItem).then((prefab: any) => {
                    let item = instantiate(prefab);
                    this.node.addChild(item);
                    item.getComponent(PropItem).Init(GameManager.Instance.propDatas[i].propType, GameManager.Instance.propDatas[i].count);
                    this.propItems.push(item.getComponent(PropItem));
                });
            }
        }

    }

}


